package com.pj.compose.ui.page.widget

import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import com.pj.compose.ui.compose.AppTopBarColumn
import com.pj.compose.ui.compose.ScrollableAppBar

@Composable
fun ScrollTablePage(navController: NavController) {
//    Sample2()
    AppTopBarColumn(
        title = "ScrollTablePage",
        backClick = { navController.popBackStack() }) {
        Sample3()
    }
}


@Composable
private fun Sample1() {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .verticalScroll(rememberScrollState())
    ) {
        SimpleScrollTable(
            modifier = Modifier
                .fillMaxWidth()
                .padding(top = 50.dp)
                .height(300.dp)
        )

        SimpleScrollTable(
            modifier = Modifier
                .fillMaxWidth()
                .padding(top = 50.dp)
                .height(300.dp)
        )

        SimpleScrollTable(
            modifier = Modifier
                .fillMaxWidth()
                .padding(top = 50.dp)
                .height(300.dp)
        )
    }
}

@Composable
private fun Sample2() {
    // 定义 ToolBar 的高度
    val toolbarHeight = 200.dp
// ToolBar 最大向上位移量
// 56.dp 参考自 androidx.compose.material AppBar.kt 里面定义的 private val AppBarHeight = 56.dp
    val maxUpPx = with(LocalDensity.current) {
        toolbarHeight.roundToPx().toFloat() - 56.dp.roundToPx().toFloat()
    }
// ToolBar 最小向上位移量
    val minUpPx = 0f
// 偏移折叠工具栏上移高度
    val toolbarOffsetHeightPx = remember { mutableStateOf(0f) }
    val nestedScrollConnection = remember {
        object : NestedScrollConnection {
            override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
                val delta = available.y
                val newOffset = toolbarOffsetHeightPx.value + delta
                // 设置 ToolBar 的位移范围
                toolbarOffsetHeightPx.value = newOffset.coerceIn(-maxUpPx, -minUpPx)
                return Offset.Zero
            }
        }
    }

    Box(
        Modifier
            .fillMaxSize()
            .nestedScroll(nestedScrollConnection) // 作为父级附加到嵌套滚动系统
    ) {
        // 列表带有内置的嵌套滚动支持，它将通知我们它的滚动
        LazyColumn(contentPadding = PaddingValues(top = toolbarHeight)) {
            items(100) { index ->
                Text(
                    "I'm item $index", modifier = Modifier
                        .fillMaxWidth()
                        .padding(16.dp)
                )
            }
        }
        ScrollableAppBar(
            title = "toolbar offset is ${toolbarOffsetHeightPx.value}",
            backgroundImageId = com.pj.compose.R.mipmap.example_image0,
            scrollableAppBarHeight = toolbarHeight,
            toolbarOffsetHeightPx = toolbarOffsetHeightPx
        )
    }
}


@Composable
private fun Sample3() {
    /*LazyColumn(
        modifier = Modifier
            .fillMaxWidth()
    ) {
       for (i in 0..5){
           item {
               SimpleScrollTable(
                   modifier = Modifier
                       .fillMaxWidth()
                       .padding(top = 50.dp)
                       .height(300.dp)
               )

           }
       }
    }*/
    Column(modifier = Modifier
        .fillMaxWidth()
        .verticalScroll(rememberScrollState())) {
        for (i in 0..5) {
            SimpleScrollTable(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(top = 50.dp)
                    .height(300.dp)
            )
        }
    }
}


@OptIn(ExperimentalFoundationApi::class)
@Composable
private fun SimpleScrollTable1(modifier: Modifier = Modifier) {
    val names = mutableListOf("序号", "归属人", "部门", "客户编号", "客户全称", "属性", "地区", "有效期", "到期时间")
    TableItem(modifier = Modifier.background(Color(0xff66ccff)), items = names)
    Row(
        modifier = modifier
            .horizontalScroll(rememberScrollState())
    ) {
        LazyColumn(
            modifier = Modifier
                .fillMaxWidth()
        ) {
            stickyHeader {
                TableItem(items = names)
            }
            for (i in 1..66) {
                item {
                    TableItem(items = names.apply { names[0] = "$i" })
                }
            }
        }
    }
}

@Composable
private fun SimpleScrollTable(modifier: Modifier = Modifier) {
    val names = mutableListOf("序号", "归属人", "部门", "客户编号", "客户全称", "属性", "地区", "有效期", "到期时间")
    val horState = rememberScrollState()
    Row(
        modifier = modifier
            .horizontalScroll(horState)
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
        ) {
            TableItem(modifier = Modifier.background(Color(0xff66ccff)), items = names)
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .verticalScroll(rememberScrollState())
            ) {
                for (i in 1..66) {
                    TableItem(items = names.apply { names[0] = "$i" })
                }
            }
        }
    }
}

@Composable
private fun TableItem(modifier: Modifier = Modifier, items: List<String>) {
    Row(modifier.height(30.dp), verticalAlignment = Alignment.CenterVertically) {
        items.forEach {
            Text(
                text = it,
                fontSize = 13.sp,
                color = Color.Black,
                modifier = Modifier.width(80.dp),
                textAlign = TextAlign.Center
            )
        }
    }
}